package com.urbansquall.ginger
{
	
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	
	public class AnimationBuilder
	{
		public function AnimationBuilder()
		{
		}
		/* 
		public static function importFromXML( a_animations:XML, a_ID:String ) : BitmapAnimation
		{
			var animationXML:XML;
			
			for each( animationXML in a_animations.elements() )
			{
				if( animationXML.ID == String( a_ID ) )
				{
					break;
				}
			}
			if( animationXML.ID != a_ID )
			{
				throw new Error( "<AnimationBuilder> That ID does not exist!" );
			}
			
			var asset:Class = Assets.instance[ animationXML.tileSheetName ];
			if( asset == null ) { throw new Error( "<AnimationBuilder> " + animationXML.tileSheetName + " does not exist!" ); }
			
			var bitmapData:BitmapData = ( new asset() ).bitmapData;
			var anim:BitmapAnimation = new BitmapAnimation( animationXML.framesOfRotation );
			anim.isLooping = Utils.stringToBoolean( animationXML.isLooping );
			var tileWidth:int = animationXML.tileWidth;
			var tileHeight:int = animationXML.tileHeight;			
			var spacerX:int = animationXML.spacerX;
			var spacerY:int = animationXML.spacerY;
			
			for each( var frameXML:XML in animationXML.frames.elements() )
			{
				var col:int = frameXML.@col;
				var row:int = frameXML.@row;
				var numFrames:int = frameXML.@numFrames;
				var ID:String = frameXML.@id;
				if( ID == "" ){ ID = null; }
				
				var posY:int = ( row * tileHeight + spacerY * row ) - tileHeight;
				var posX:int = ( col * tileWidth + spacerX * col ) - tileWidth;
				var rect:Rectangle = new Rectangle( posX, posY, tileWidth, tileHeight );
				anim.addFrame( numFrames, bitmapData, rect, ID );
			}
			
			return anim;
		}
		 */
		public static function importTileSheet( a_bitmapData:BitmapData, a_rowsToCopy:int, a_tileWidth:int, 
			a_tileHeight:int, a_framesOfRotation:int=1, a_spacerX:int=0, a_spacerY:int=0, a_startRow:int=0 ) : BitmapAnimation
		{
			var animation:BitmapAnimation = new BitmapAnimation( a_framesOfRotation );
			var posX:int;
			var posY:int;
			var x:int;
			var y:int;
			var rect:Rectangle;
			var numColumns:int = int( a_bitmapData.width / a_tileWidth );
			
			for( y = a_startRow; y < a_startRow + a_rowsToCopy; y++ )
			{
				posY = ( y * a_tileHeight + a_spacerY * y ) - a_tileHeight;
				for( x = 1; x <= numColumns; x++ )
				{
					posX = ( x * a_tileWidth + a_spacerX * x ) - a_tileWidth;
					rect = new Rectangle( posX, posY, a_tileWidth, a_tileHeight );
					animation.addFrame( 1, a_bitmapData, rect );
				}
			}
			
			return animation;	
		}
		
		public static function importFromTileSheet(
                       a_bitmapData:BitmapData, a_tileWidth:int, a_tileHeight:int, a_framesOfRotation:int=1,
                       a_startRow:int=0, a_rowsToCopy:int = -1,
                       a_startColumn:int=1, a_columnsToCopy:int = -1,
                       a_spacerX:int=0, a_spacerY:int=0 ) : BitmapAnimation
               {
                       var animation:BitmapAnimation = new BitmapAnimation( a_framesOfRotation );
                       var posX:int;
                       var posY:int;
                       var x:int;
                       var y:int;
                       var rect:Rectangle;

                       if( a_columnsToCopy == -1 )
                       {
                               var a_columnsToCopy:int = int( a_bitmapData.width / a_tileWidth );
                       }
                       if( a_rowsToCopy == -1 )
                       {
                               a_rowsToCopy = 1;
                       }

                       trace( "Counts: " + a_columnsToCopy + ":" +  a_rowsToCopy  );

                       for( y = a_startRow ; y < a_startRow + a_rowsToCopy; y++ )
                       {
                               posY = ( y * a_tileHeight + a_spacerY * y );
                               for( x = a_startColumn; x < a_startColumn + a_columnsToCopy; x++ )
                               {
                                       posX = ( x * a_tileWidth + a_spacerX * x );
                                       rect = new Rectangle( posX, posY, a_tileWidth, a_tileHeight );
                                       animation.addFrame( 1, a_bitmapData, rect );
                               }
                       }

                       return animation;
               }

	}
}